#!/bin/bash

find_project_root() {
    local dir="$1"
    while [ "$dir" != "/" ]; do
        if [ -f "$dir/pubspec.yaml" ]; then
            echo "$dir"
            return 0
        fi
        dir=$(dirname "$dir")
    done
    return 1
}

ROOT_DIR=$(find_project_root "$(pwd)") || {
    echo "ERROR: Not in a Flutter project" >&2
    exit 1
}

cd "$ROOT_DIR" || exit 1

if [ ! -f "iconfont.yaml" ]; then
    echo "ERROR: iconfont.yaml not found in project root" >&2
    exit 1
fi

echo "Generating icons..."
dart run iconfont_convert --config iconfont.yaml || {
    echo "ERROR: Check iconfont.yaml format" >&2
    exit 1
}

echo "SUCCESS: Icons generated"